package me.jessyan.armscomponent.app.mvp.presenter.learn;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.RxLifecycleUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.armscomponent.app.mvp.model.entity.SignResponse;
import me.jessyan.armscomponent.commonsdk.http.BaseResponse;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import javax.inject.Inject;

import me.jessyan.armscomponent.app.mvp.contract.learn.SignActivityContract;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description: 签到
 * <p>
 * Created by MVPArmsTemplate on 10/21/2021 10:43
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class SignActivityPresenter extends BasePresenter<SignActivityContract.Model, SignActivityContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SignActivityPresenter (SignActivityContract.Model model, SignActivityContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void sign() {
        mModel.sign()
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();//显示下拉刷新的进度条
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<BaseResponse<Object>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseResponse<Object> datas) {
                        if(datas != null){
                            if (datas.success()) {
                                mRootView.showMessage("打卡成功");
                                loadSignList(); //重新加载打卡记录
                            }else {
                                BaseResponse.Errmsg errmsg = datas.getErrmsg();
                                if(null != errmsg){
                                    mRootView.showMessage(errmsg.getEn());
                                }else{
                                    mRootView.showMessage("接口返回参数异常");
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable t) {
                        super.onError(t);
                        mRootView.showMessage("接口异常,"+t.getMessage());
                    }
                });
    }

    public void loadSignList() {
        long startTime = Utils.getTimeOfWeekStart();
        long endTime = Utils.getTimeOfWeekEnd();
        mModel.loadSigns(startTime, endTime)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();//显示下拉刷新的进度条
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<BaseResponse<SignResponse>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseResponse<SignResponse> datas) {
                        if(datas != null){
                            if (datas.success()) {
                                mRootView.rendSignList(datas.getResult());
                            }else {
                                BaseResponse.Errmsg errmsg = datas.getErrmsg();
                                if(null != errmsg){
                                    mRootView.showMessage(errmsg.getEn());
                                }else{
                                    mRootView.showMessage("接口返回参数异常");
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable t) {
                        super.onError(t);
                        mRootView.showMessage("接口异常,"+t.getMessage());
                    }
                });
    }
}
